/*
 * Copyright(c) 2020-2100 万达信息股份有限公司 版权所有
 * WONDERS INFORMATION CO., LTD. ALL RIGHTS RESERVED.
 */

package com.wondersgroup.controller;

import com.netflix.loadbalancer.IRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author: huym
 * @version: 1.0
 * @date: 2020/11/19
 */
@RestController
public class UserController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    /************************************************点对点，且没有负载均衡*************************************************/
    @GetMapping("/user/showProductMsg")
    public String showProductMsg(){
        // 第一种服务端调用方式 服务地址：http://localhost:9998/product/showMsg
        RestTemplate restTemplate = new RestTemplate();
        String msg = restTemplate.getForObject("http://localhost:9998/product/showMsg", String.class);
        return msg;
    }

    @GetMapping("/user/findAllProduct")
    public String findAllProduct(){
        RestTemplate restTemplate = new RestTemplate();
        //Map map = restTemplate.getForObject("http://localhost:9998/product/findAll", Map.class);
        String msg = restTemplate.getForObject("http://localhost:9998/product/findAll", String.class);
        return msg;
    }

    @GetMapping("/user/findAllProductWithMap")
    public Map findAllProductWithMap(){
        RestTemplate restTemplate = new RestTemplate();
        Map map = restTemplate.getForObject("http://localhost:9998/product/findAll", Map.class);
        return map;
    }
    /************************************************点对点，且没有负载均衡*************************************************/

    /**
     * ribbon调用方式1：DiscoveryClient
     * 发现服务，但是不具备负载均衡
     *
     * @return
     */
    @GetMapping("/user/discoveryClient")
    public List<ServiceInstance> discoveryClient(){
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("product");
        return serviceInstances;
    }

    /**
     * ribbon调用方式2：LoadBalancerClient
     * 发现服务，并按照默认轮询策略获取服务
     *
     * @return
     */
    @GetMapping("/user/loadBalancerClient")
    public ServiceInstance loadBalancerClient(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("product");
        return serviceInstance;
    }

    /**
     * RestTemplate和loadBalancerClient配合使用，@LoadBalanced注解
     * 需要定义RestTemplateConfig的实例，交由Spring容器管理
     *
     * @return
     */
    @GetMapping("/user/findAllProduct2")
    public String findAllProduct2(){
        return restTemplate.getForObject("http://product/product/findAll",String.class);
    }

    public void iRule(){
        IRule iRule;
    }

}
